package nocturnos.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import nocturnos.domain.Puntos;

public class JDBCPuntosDAO implements iPuntosDAO {

	@Override
	public void update (Integer idGrupo, Integer totalPuntos){

//		UPDATE puntos SET totalPuntos = ? WHER idGrupo=? 		

		ConnectionManager cm2 = ConnectionManager.getInstance();
		Connection con2 = (Connection) cm2.checkOut();
		
		String sql = "UPDATE puntos SET totalPuntos = ? WHERE idGrupo=? ";
        PreparedStatement stmt = null;

        try {
            stmt = con2.prepareStatement(sql);

            stmt.setInt(1, totalPuntos);
            stmt.setInt(2, idGrupo);
            
            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }


		
	}
	
	public void delete(Connection con, String puntosOID) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void insert(Connection con, String puntosOID) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Integer select(Integer idGrupo) {
		// TODO Auto-generated method stub
		
		ConnectionManager cm = ConnectionManager.getInstance();
		Connection con = (Connection) cm.checkOut();
			
			String sql2 = "SELECT DISTINCT totalPuntos FROM puntos WHERE idGrupo=?;";
	        PreparedStatement stmt2 = null;
	        ResultSet result2 = null;
	        Integer valor=0;
	        
	    try{    	
			stmt2 = con.prepareStatement(sql2);
	        stmt2.setInt(1, idGrupo);
	        result2 = stmt2.executeQuery();
	        
	        result2.next();
	        valor = result2.getInt("totalPuntos");
	        
			
	}catch (SQLException e) {
	    System.out.println("Message: " + e.getMessage());
	    System.out.println("SQLState: " + e.getSQLState());
	    System.out.println("ErrorCode: " + e.getErrorCode());
	} finally {
	    try {
	        if (result2 != null) {
	            result2.close();
	        }
	        if (stmt2 != null) {
	            stmt2.close();
	        }
	    } catch (SQLException e) {
	    }
	}


		
		

		
		
		
		return valor;		
	
	
	}

	@Override
	public Integer select(Connection con, Integer idGrupo) {
		// TODO Auto-generated method stub
		return null;
	}


}
